/**
 * Created by three on 14-11-4.
 */
'use strict';

// directives for localization
angular.module('th.localize', ['th.logger'])

    .factory('localize', ['$http', '$rootScope','logger', function ($http, $rootScope, logger) {
        var localize = {
            currentLocaleData: {},
            currentLang: {},
            customerConf:{},
            configLang: function (conf) {
                localize.customerConf.langPath = conf.langPath;
                localize.customerConf.defaultLang = conf.defaultLang;
                localize.customerConf.langMap =  conf.langMap;
                return localize;
            },
            setLang: function (lang) {
                var langUrl = undefined;
                try {
                    langUrl = localize.getLangUrl(lang);
                } catch (e) {
                    logger.info("多语言环境配置错误！")
                    return false;
                }
                logger.info('加载语言：'+langUrl);
                if(langUrl != undefined) {
                    $http({method: 'GET', url: langUrl, cache: false})
                        .success(function (data) {
                            localize.currentLocaleData = data;
                            localize.currentLang = lang;
                            $rootScope.$broadcast('localizeLanguageChanged');
                        }).error(function (data) {
                            logger.info('Error updating language!');
                            logger.info(data);
                        })
                }
                return localize;
            },
            getLangUrl: function (lang) {
                if(localize.customerConf.langMap[lang]) {
                    return localize.customerConf.langPath+localize.customerConf.langMap[lang];
                }
                if(!localize.customerConf.langMap[localize.customerConf.defaultLang]) {
                    throw new Error("语言环境配置错误");
                }
                return localize.customerConf.langPath+localize.customerConf.langMap[localize.customerConf.defaultLang];
            },
            localizeText: function (sourceText) {
                return localize.currentLocaleData[sourceText];
            }
        };

        return localize;
    }])

    .directive('localize', ['localize', function (localize) {
        var localizeFun = function (scope, element, attrs) {
            if(!attrs.hasOwnProperty('localize')) { // dom使用方式
                if(!attrs['sourceText']) {
                    attrs['sourceText'] = element.text();
                }
                var localizedText = localize.localizeText(attrs['sourceText']);
                element.text(localizedText);
            } else { // 属性使用方式
                var localizedText = localize.localizeText(scope.sourceText);
                if (element.is('input, textarea')) element.attr('placeholder', localizedText)
                else element.text(localizedText);
            }
        };
        return {
            restrict: 'EA',
            scope: {
                sourceText: '@localize'
            },
            link: function (scope, element, attrs) {

                localizeFun(scope, element, attrs);

                scope.$on('localizeLanguageChanged', function () {
                    localizeFun(scope, element, attrs);
                });
            }
        }
    }])

    .filter('localize', ['localize', function (localize) {
        return function (text) {
            return localize.localizeText(text);
        }
    }])
;